<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Admin\Leave;
use Auth;


class LeaveController extends Controller
{
    //
    public function index() 
    {
        $data = Leave::where('pid', '0')->get();
        return view('home.leave.index', compact('data'));
    }

    public function tump(Request $request) 
    {
        $id = $request->get('id');
        $d = $request->get('d');
        $count = Leave::find($id)->tump_up;
        
        if ($d == 'like') {
           $result = Leave::where('id', $id)->update(['tump_up' => $count+1]);
        } else {
            $result = Leave::where('id', $id)->update(['tump_up' => $count-1]);
        }
         $count = Leave::find($id)->tump_up;
        if ($result == '1') {
            return  response()->json(
                [
                    'info' => '1',
                    'message' => '点赞成功',
                    'count' => $count
                ]
            );
        } else {
            return  response()->json(
                [
                    'info' => '2',
                    'message' => '点赞失败',
                    'count' => $count
                ]
            );
        }

    }
    public function add(Request $request) 
    {
        if ($request->method() == 'POST') {
             $comment = $request->comment;
             $data['comment'] = $comment;
             $data['member_id'] = Auth::guard('member') -> user() -> id;
             $data['pid'] = '0';
            $result =  Leave::create($data);
            if ($result) {
                return '1';
            } 
        } else {
            return view('home.leave.add');
        }
       
    }

    
}
